<template>
  <el-dialog
    :title="!dataForm.caseId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="80px"
             @keyup.enter.native="dataFormSubmit()">
      <el-form-item label="用例名称" prop="caseName">
        <el-input v-model="dataForm.caseName" placeholder="用例名称"></el-input>
      </el-form-item>
      <el-form-item label="项目" prop="project">
        <el-input v-model="dataForm.project" placeholder="项目"></el-input>
      </el-form-item>
      <el-form-item label="模块" prop="module">
        <el-input v-model="dataForm.module" placeholder="模块"></el-input>
      </el-form-item>
      <el-form-item label="执行人" prop="operator">
        <el-input v-model="dataForm.operator" placeholder="执行人"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      <el-button @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>

export default {
  data() {
    return {
      visible: false,
      roleList: [],
      dataForm: {},
      dataRule: {
        caseName: [
          {required: true, message: '用例名称不能为空', trigger: 'blur'}
        ],
        project: [
          {required: true, message: '项目不能为空', trigger: 'blur'}
        ],
        module: [
          {required: true, message: '模块不能为空', trigger: 'blur'}
        ],
        operator: [
          {required: true, message: '执行人不能为空', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    init(caseId) {
      this.dataForm.caseId = caseId || 0
      this.$http({
        url: this.$http.adornUrl('/test/stress/list'),
        method: 'get',
        params: this.$http.adornParams({
          page: 1,
          limit: 10,
        })
      }).then(({data}) => {
        this.roleList = data && data.code === 0 ? data.list : []
      }).then(() => {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      }).then(() => {
        if (this.dataForm.caseId) {
          this.$http({
            url: this.$http.adornUrl(`/test/stress/info/${this.dataForm.caseId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm = data.stressCase
              console.log(this.dataForm)
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/test/stress/${!this.dataForm.caseId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData(
              this.dataForm
          )
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
